﻿using DFGK.WMS.Common.Result;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Repository;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Net.Http;

namespace DFGK_WMS.WebAPI.Controllers.盘点管理
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TInventoryQueryController : ControllerBase
    {
        private readonly ITInventoryQuery tTInventoryQuery;
        private readonly ITStocktakingOrderRepository TStocktakingOrderRepository;
    
        private readonly ITCellRepository tCellRepository1;
        public TInventoryQueryController(ITInventoryQuery tTInventoryQuery, ITStocktakingOrderRepository tStocktakingOrderRepository,ITCellRepository tCellRepository)
        {
            this.tTInventoryQuery = tTInventoryQuery;
            this.TStocktakingOrderRepository = tStocktakingOrderRepository;
            this.tCellRepository1 = tCellRepository;
        }
        /// <summary>
        /// 海宁盘点列表计划添加
        /// </summary>
        /// <param name="inventories"></param>
        /// <param name="planid"></param>
        /// <param name="pagenumber"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetTInventorywuliao(PandianDanhaodto dto)
        {
            bool pandian = tTInventoryQuery.GetTInventorywuliao(dto.Items, dto.planId, dto.planNumber);
            ApiBaseResult baseResult = new ApiBaseResult();
            if (!pandian)
            {
                baseResult.Code = "500";
                baseResult.Success = false;
                baseResult.Message = "数据错误";
                return Ok(baseResult);
            }
            baseResult.Code = "200";
            baseResult.Success = true;
            baseResult.Message = "";
            return new JsonResult(baseResult);
        }
        /// <summary>
        /// 查询全部盘点pda上信息
        /// </summary>
        /// <param name="planid"></param>
        /// <param name="Ordernum"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPDApandianlist(long? planid, string? Ordernum, int pageindex, int pagesize)
        {
            var list = TStocktakingOrderRepository.GetPDApandianlist(planid, Ordernum, pageindex, pagesize, out var totalt);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "200"));
            }
            return Ok(BaseResult.Result("400", "错误"));
        }
        /// <summary>
        /// 查询pda上盘点列表数据
        /// </summary>
        /// <param name="planid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPDAlistyi(int pageindex=1, int pagesize=10)
        {

            int totalt;
            var list = TStocktakingOrderRepository.GetPDAlistyi(out totalt,pageindex, pagesize);

            ApiItemsResult baseResult = new ApiItemsResult();
            if (list != null)
            {
                baseResult.code = "200";
                baseResult.count = totalt;
                baseResult.message = "已查询";
                baseResult.data= list;
                return Ok(baseResult);
            }
            baseResult.code = "500";
            baseResult.count = totalt;
            baseResult.message = "查询失败";
            return Ok(baseResult);
        }
        /// <summary>
        /// 根据盘点单号回传状态
        /// </summary>
        /// <param name="ordernum"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult GetXiugaizhuangtai(string ordernum)
        {
            string meessage;
            bool list = tTInventoryQuery.Getpandianxiugai(ordernum, out meessage);
            ApiBaseResult baseResult = new ApiBaseResult();
            if (list)
            {
                baseResult.Code = "200";
                baseResult.Success = true;
                baseResult.Message = meessage;
                return Ok(BaseResult.Result(list, "200"));
            }
            baseResult.Code = "500";
            baseResult.Success = false;
            baseResult.Message = meessage;
            return Ok(baseResult);
        }
        //调用接口返回token值
        /*[HttpGet]
        public async Task<IActionResult> Getyonghuliebiao()
        {
            try
            {
                HttpResponseMessage response = await httpClient.GetAsync("http://192.168.1.60:1988/api/RFID/GetUserList");
                if (response.IsSuccessStatusCode)
                {
                    string resonry = await response.Content.ReadAsStringAsync();//返回数据成功
                    return Ok(resonry);
                }
                else
                {
                    return StatusCode((int)response.StatusCode);//请求失败
                }
            }
            catch (Exception)
            {

                throw new Exception();
            }

        }*/
        /// <summary>
        /// 返回所有pda上盘点列表查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPdaxianshishuju(string? name = "", int pageindex = 1, int pagesize = 5)
        {
            string meessage;
            int totalCount;
            var items = tTInventoryQuery.GetPdaxianshishuju(name, pageindex, pagesize, out totalCount);
            ApiItemsResult baseResult = new ApiItemsResult();
            if (items != null)
            {
                baseResult.code = "200";
                baseResult.message = "获取成功!";
                baseResult.data = items;
                baseResult.count = totalCount;
                return Ok(baseResult);
            }
            
            baseResult.code = "500";
            baseResult.data = false;
            baseResult.message = "查询失败";
            return Ok(baseResult);
        }
        //[HttpGet]
        //public IActionResult TIanjianExcel()
        //{
        //   // var Excelshuju = tCellRepository1.ReadExcelData(path);
        //    bool shuju = tCellRepository1.Addlistpliang();
        //    ApiItemsResult baseResult = new ApiItemsResult();
        //    if (shuju)
        //    {
        //        baseResult.code = "200";
        //        baseResult.message = "获取成功!";
        //        return Ok(baseResult);
        //    }
        //    baseResult.code = "500";
        //    baseResult.items = false;
        //    baseResult.message = "查询失败";
        //    return Ok(baseResult);
        //}
       

    }
}
